package com.hkts.naturalgas.base.service.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hkts.naturalgas.base.entity.RoastingoneRstwoRsjone;
import com.hkts.naturalgas.base.entity.RoastingoneRstwoRsjthree;
import com.hkts.naturalgas.base.entity.RoastingoneRstwoRsjtwo;
import com.hkts.naturalgas.base.mapper.RoastingoneRstwoRsjtwoMapper;
import com.hkts.naturalgas.base.service.IRoastingoneRstwoRsjtwoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkts.naturalgas.utils.CurveUtils;
import com.hkts.naturalgas.utils.DataHandlerUtils;
import com.hkts.naturalgas.utils.SnowFlakeUtil;
import com.hkts.naturalgas.utils.resultMapUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yck
 * @since 2021-06-21
 */
@Service
@Transactional
public class RoastingoneRstwoRsjtwoServiceImpl extends ServiceImpl<RoastingoneRstwoRsjtwoMapper, RoastingoneRstwoRsjtwo> implements IRoastingoneRstwoRsjtwoService {


    @Autowired
    private RoastingoneRstwoRsjtwoMapper roastingoneRstwoRsjtwoMapper;

    @Override
    public IPage<RoastingoneRstwoRsjtwo> selectPage(Page<RoastingoneRstwoRsjtwo> page, RoastingoneRstwoRsjtwo roastingoneRstwoRsjtwo){
            QueryWrapper<RoastingoneRstwoRsjtwo> query=Wrappers.query();
            QueryWrapper<RoastingoneRstwoRsjtwo> entityWrapper=getEntityWrapper(query, roastingoneRstwoRsjtwo);
            return roastingoneRstwoRsjtwoMapper.selectPage(page,entityWrapper);
    }


    @Override
    public List<RoastingoneRstwoRsjtwo> selectList(RoastingoneRstwoRsjtwo roastingoneRstwoRsjtwo){
            QueryWrapper<RoastingoneRstwoRsjtwo> query=Wrappers.query();
            QueryWrapper<RoastingoneRstwoRsjtwo> entityWrapper=getEntityWrapper(query, roastingoneRstwoRsjtwo);
            return roastingoneRstwoRsjtwoMapper.selectList(entityWrapper);
    }

    /**
     *  公共查询条件
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<RoastingoneRstwoRsjtwo> getEntityWrapper(QueryWrapper<RoastingoneRstwoRsjtwo> entityWrapper,RoastingoneRstwoRsjtwo roastingoneRstwoRsjtwo){
            //条件拼接
            return entityWrapper;
    }

    @Override
    public IPage<RoastingoneRstwoRsjtwo> selectPageByTime(Page<RoastingoneRstwoRsjtwo> page, String startDate, String endDate){
        QueryWrapper<RoastingoneRstwoRsjtwo> query = Wrappers.query();
        query.lambda().apply(!"".equals(startDate),
                "time >= '" + startDate + "'")
                .apply(!"".equals(endDate),
                        "time <= '" + endDate + "'")
                .orderByDesc(RoastingoneRstwoRsjtwo::getTime);
        IPage<RoastingoneRstwoRsjtwo> hktsTailGasPage = roastingoneRstwoRsjtwoMapper.selectPage(page, query);
        return hktsTailGasPage;
    }
    @Override
    public IPage<RoastingoneRstwoRsjtwo> getBrokenLineGraph(Page<RoastingoneRstwoRsjtwo> page,String startDate, String endDate){
        QueryWrapper<RoastingoneRstwoRsjtwo> query = Wrappers.query();
        query.lambda().apply(!"".equals(startDate),
                "time >= ' " + startDate + " ' " )
                .apply(!"".equals(endDate),
                        "time <= ' " + endDate + " ' ")
                .orderByAsc(RoastingoneRstwoRsjtwo::getTime);
        IPage roastingoneRsoneRsjonePage = roastingoneRstwoRsjtwoMapper.selectPage(page,query);
        List<RoastingoneRstwoRsjtwo> records = roastingoneRsoneRsjonePage.getRecords();
        List<Map<String,Object>> resultDate = records.stream().map(t -> assemblyCargoData(t)).collect(Collectors.toList());
        Map<String, List<Object>> stringListMap = resultMapUtils.resultData(resultDate);
        List<Object> list1 = resultMapUtils.getList(resultDate);
        List<Object> listName = resultMapUtils.getListName(resultDate);
        Map<String,Object> data = new HashMap<>();
        data.put("list",listName);
        data.put("records",stringListMap);
        data.put("times",list1);
        List<Object> resultList = new ArrayList<>();
        resultList.add(data);
        IPage iPage = roastingoneRsoneRsjonePage.setRecords(resultList);
        return iPage;
    }

    public Map<String,Object> assemblyCargoData(RoastingoneRstwoRsjtwo vo){
        Map<String,Object> data = new LinkedHashMap<>();
        data.put("火道温度1(℃)",vo.getHdw1());
        data.put("火道温度2(℃)",vo.getHdw2());
        data.put("火道温度3(℃)",vo.getHdw3());
        data.put("火道温度4(℃)",vo.getHdw4());
        data.put("火道温度5(℃)",vo.getHdw5());
        data.put("火道温度6(℃)",vo.getHdw6());
        data.put("火道温度7(℃)",vo.getHdw7());
        data.put("火道温度8(℃)",vo.getHdw8());
        data.put("火道温度9(℃)",vo.getHdw9());
        data.put("火道温度10(℃)",vo.getHdw10());
        data.put("火道温度11(℃)",vo.getHdw11());
        data.put("火道压力1",vo.getHdy1());
        data.put("火道压力2",vo.getHdy2());
        data.put("火道压力3",vo.getHdy3());
        data.put("火道压力4",vo.getHdy4());
        data.put("火道压力5",vo.getHdy5());
        data.put("火道压力6",vo.getHdy6());
        data.put("火道压力7",vo.getHdy7());
        data.put("火道压力8",vo.getHdy8());
        data.put("火道压力9",vo.getHdy9());
        data.put("火道压力10",vo.getHdy10());
        data.put("火道压力11",vo.getHdy11());
        data.put("料箱1温度(℃)",vo.getLx1w());
        data.put("料箱2温度(℃)",vo.getLx2w());
        data.put("料箱3温度(℃)",vo.getLx3w());
        data.put("燃气压力",vo.getYl());
        data.put("time",vo.getTime());
        return data;
    }

    @Override
    public void addHostoryData(String startDate, String endDate) throws ParseException {
            CurveUtils utils=new CurveUtils();
            utils.setStartDate(startDate);
            utils.getGrowthRate().add(8.75f);
            utils.getGrowthRate().add(10f);
            utils.getGrowthRate().add(10.5294f);
            utils.getGrowthRate().add(0.9675f);
            utils.getGrowthRate().add(0f);
            //时间段
            utils.getTimeSlot().add(8);
            utils.getTimeSlot().add(7);
            utils.getTimeSlot().add(17);
            utils.getTimeSlot().add(32);
            utils.getTimeSlot().add(32);
            utils.setEndDate(endDate);
            utils.getData(780f);

            for(int i=0;i<utils.getDateList().size();i++){
                RoastingoneRstwoRsjtwo one=new RoastingoneRstwoRsjtwo(String.valueOf(SnowFlakeUtil.getFlowIdInstance().nextId())
                        ,utils.getDateList().get(i),
                        DataHandlerUtils.getData(utils.getDataResult().get(i)),
                        DataHandlerUtils.getData(utils.getDataResult().get(i)),
                        DataHandlerUtils.getData(utils.getDataResult().get(i)),
                        DataHandlerUtils.getData(utils.getDataResult().get(i)),
                        DataHandlerUtils.getData(utils.getDataResult().get(i)),
                        DataHandlerUtils.getData(utils.getDataResult().get(i)),
                        DataHandlerUtils.getData(utils.getDataResult().get(i)),
                        DataHandlerUtils.getData(utils.getDataResult().get(i)),
                        DataHandlerUtils.getData(utils.getDataResult().get(i)),
                        DataHandlerUtils.getData(utils.getDataResult().get(i)),
                        DataHandlerUtils.getData(utils.getDataResult().get(i)),
                        0f,0f,0f,0f,0f,0f,0f,0f,0f,0f,0f,0f,0f,0f,0f
                );
                roastingoneRstwoRsjtwoMapper.insert(one);
        }
    }

    @Override
    public void handlerPresserData(String endDate) {
        QueryWrapper<RoastingoneRstwoRsjtwo> query=Wrappers.query();
        query.le("time",endDate);
        query.isNotNull("hdw1");
        List<RoastingoneRstwoRsjtwo> roastingonePyjones = roastingoneRstwoRsjtwoMapper.selectList(query);
        for(int i=0;i<roastingonePyjones.size();i++){
            roastingonePyjones.get(i).setHdy1(DataHandlerUtils.getPressureRandomNumber());
            roastingonePyjones.get(i).setHdy2(DataHandlerUtils.getPressureRandomNumber());
            roastingonePyjones.get(i).setHdy3(DataHandlerUtils.getPressureRandomNumber());
            roastingonePyjones.get(i).setHdy4(DataHandlerUtils.getPressureRandomNumber());
            roastingonePyjones.get(i).setHdy5(DataHandlerUtils.getPressureRandomNumber());
            roastingonePyjones.get(i).setHdy6(DataHandlerUtils.getPressureRandomNumber());
            roastingonePyjones.get(i).setHdy7(DataHandlerUtils.getPressureRandomNumber());
            roastingonePyjones.get(i).setHdy8(DataHandlerUtils.getPressureRandomNumber());
            roastingonePyjones.get(i).setHdy9(DataHandlerUtils.getPressureRandomNumber());
            roastingonePyjones.get(i).setHdy10(DataHandlerUtils.getPressureRandomNumber());
            roastingonePyjones.get(i).setHdy11(DataHandlerUtils.getPressureRandomNumber());
        }
        this.saveOrUpdateBatch(roastingonePyjones);
    }

    @Override
    public void handlerCurveData(String startDate, String endDate, Float mbt, String order) {
        QueryWrapper<RoastingoneRstwoRsjtwo> query=Wrappers.query();
        query.le("time",endDate);
        query.ge("time",startDate);
        List<RoastingoneRstwoRsjtwo> roastingonePyjones = roastingoneRstwoRsjtwoMapper.selectList(query);
        List<Float> list=new ArrayList<Float>();
        if(order.equals("desc")){
            list=  DataHandlerUtils.getTemperatureCurve(mbt,roastingonePyjones.get(0).getHdw1(),roastingonePyjones.size(),order);
        }else{
            list=  DataHandlerUtils.getTemperatureCurve(mbt,roastingonePyjones.get(roastingonePyjones.size()-1).getHdw1(),roastingonePyjones.size(),order);
        }
        for(int i=0;i<roastingonePyjones.size();i++){
            roastingonePyjones.get(i).setHdw1( DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setHdw2(DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setHdw3( DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setHdw4(DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setHdw5( DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setHdw6(DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setHdw7( DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setHdw8(DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setHdw9( DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setHdw10(DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setHdw11( DataHandlerUtils.getData(list.get(i)));

        }
        this.saveOrUpdateBatch(roastingonePyjones);
    }
}
